package com.bjpowernode.controller;

import com.bjpowernode.entity.Economics;
import com.bjpowernode.entity.Task;
import com.bjpowernode.service.EconomicsService;
import com.bjpowernode.service.StatisticsService;
import com.bjpowernode.service.TaskService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * 处理计划完成度相关请求
 *
 * @author 刘逸晖
 */
@RequestMapping
public class ProgressController {

    /**
     * 处理统计相关业务
     */
    private StatisticsService statisticsService;

    /**
     * 处理财务计划相关任务
     */
    private TaskService taskService;

    /**
     * 处理收支记录相关业务
     */
    private EconomicsService economicsService;

    /**
     * 查询本月计划实现度
     *
     * @return 模型和试图
     */
    @RequestMapping(value = "/myWeb/progress.do")
    public ModelAndView progress() {
        //查询当前月份
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        int month = calendar.get(Calendar.MONTH) + 1;

        //查询本月财务计划
//        List<Task> tasks = this.taskService.selectTasksByMonth(month);
        //弄错了，要的是本月收支记录。

        List<Economics> economics = this.statisticsService.selectEconomicsOfCurrentMonth();

//        查询本月财务计划的总金额
        Integer target = this.taskService.selectTotalMoneyByMonth(month);

        //计算本月余额
        Integer balance = this.statisticsService.selectBalanceOfMonth();

        if (economics == null || target == null || balance == null) {
            ModelAndView modelAndView = new ModelAndView("info");
            modelAndView.addObject("info", "抱歉，查询失败");
            return modelAndView;
        }

        String comment = "";

        if (target > balance) {//未完成
            comment = "还有" + (target - balance) + "元的计划未完成";
        } else if (balance > target) {//已完成
            comment = "您已超额完成" + (balance - target) + "元";
        } else {
            comment = "正好完成任务";
        }

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("status", comment)
                .addObject("list", economics)
                .addObject("task", target)
                .addObject("balance", balance)
                .setViewName("progress");
        return modelAndView;
    }

    public StatisticsService getStatisticsService() {
        return statisticsService;
    }

    public void setStatisticsService(StatisticsService statisticsService) {
        this.statisticsService = statisticsService;
    }

    public TaskService getTaskService() {
        return taskService;
    }

    public void setTaskService(TaskService taskService) {
        this.taskService = taskService;
    }

    public EconomicsService getEconomicsService() {
        return economicsService;
    }

    public void setEconomicsService(EconomicsService economicsService) {
        this.economicsService = economicsService;
    }
}
